package com.grass.p019lv;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.androidx.p013lv.base.BaseSdk;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.utils.FileListUtils;
import com.androidx.p013lv.base.utils.SpUtils;
import com.google.android.exoplayer2.source.LoopingMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.RawResourceDataSource;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.CacheSpan;
import com.google.android.exoplayer2.upstream.cache.ContentMetadata;
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import com.grass.p019lv.event.EventBusEvent;
import com.grass.p019lv.event.EventBusUtils;

import java.io.File;
import java.util.Map;
import java.util.NavigableSet;

import p031tv.danmaku.ijk.media.exo2.source.GSYExoHttpDataSourceFactory;
import tv.danmaku.ijk.media.exo2.ExoMediaSourceInterceptListener;

/* renamed from: com.grass.lv.AndroidMediaSourceInterceptListener */
public class AndroidMediaSourceInterceptListener implements ExoMediaSourceInterceptListener {
    private static final long DEFAULT_MAX_SIZE = 536870912;
    private static final String TAG = "FxMediaSourceInterceptListener";
    private static Cache mCache = null;
    private static int sHttpConnectTimeout = -1;
    private static int sHttpReadTimeout = -1;
    private static boolean sSkipSSLChain;
    private boolean isCached = false;
    private Context mAppContext;
    private String mDataSource;
    private Map<String, String> mMapHeadData;

    public AndroidMediaSourceInterceptListener(Context context) {
        this.mAppContext = context;
    }

    @Override // p031tv.danmaku.ijk.media.exo2.ExoMediaSourceInterceptListener
    public MediaSource getMediaSource(String str, boolean z, boolean z2, boolean z3, File file) {
        EventBusUtils.sendEvent(new EventBusEvent(EventBusUtils.EventCode.SERVICE_STOP));
        this.mDataSource = str;
        Uri parse = Uri.parse(str);
        if ("android.resource".equals(parse.getScheme())) {
            DataSpec dataSpec = new DataSpec(parse);
            final RawResourceDataSource rawResourceDataSource = new RawResourceDataSource(this.mAppContext);
            try {
                rawResourceDataSource.open(dataSpec);
            } catch (RawResourceDataSource.RawResourceDataSourceException e) {
                e.printStackTrace();
            }
            return new ProgressiveMediaSource.Factory(new DataSource.Factory() {
                /* class com.grass.p019lv.AndroidMediaSourceInterceptListener.C28591 */

                @Override // com.google.android.exoplayer2.upstream.DataSource.Factory
                public DataSource createDataSource() {
                    return rawResourceDataSource;
                }
            }).createMediaSource(parse);
        }
        HlsMediaSource createMediaSource = new HlsMediaSource.Factory(getDataSourceFactoryCache(this.mAppContext, z2, z, file)).createMediaSource(parse);
        return z3 ? new LoopingMediaSource(createMediaSource) : createMediaSource;
    }

    @Override
    public HttpDataSource.BaseFactory getHttpDataSourceFactory(String userAgent,
                                                               @Nullable TransferListener listener, int connectTimeoutMillis, int readTimeoutMillis, boolean allowCrossProtocolRedirects) {
        return null;
    }

    @SuppressLint("WrongConstant")
    private DataSource.Factory getDataSourceFactoryCache(Context context, boolean z, boolean z2, File file) {
        Cache cacheSingleInstance;
        if (!z || (cacheSingleInstance = getCacheSingleInstance(context, file)) == null) {
            return getDataSourceFactory(context, z2);
        }
        this.isCached = resolveCacheState(cacheSingleInstance, this.mDataSource);
        return new CacheDataSourceFactory(cacheSingleInstance, getDataSourceFactory(context, z2), 2);
    }

    public static synchronized Cache getCacheSingleInstance(Context context, File file) {
        Cache cache;
        synchronized (AndroidMediaSourceInterceptListener.class) {
            String absolutePath = context.getFilesDir().getAbsolutePath();
            if (file != null) {
                absolutePath = file.getAbsolutePath();
            }
            if (mCache == null) {
                String str = absolutePath + File.separator + "exo";
                if (!SimpleCache.isCacheFolderLocked(new File(str))) {
                    mCache = new SimpleCache(new File(str), new LeastRecentlyUsedCacheEvictor(536870912));
                }
            }
            cache = mCache;
        }
        return cache;
    }

    private DataSource.Factory getDataSourceFactory(Context context, boolean z) {
        return new DefaultDataSourceFactory(context, z ? null : new DefaultBandwidthMeter(), getHttpDataSourceFactory(context, z));
    }

    private DataSource.Factory getHttpDataSourceFactory(Context context, boolean z) {
        int i = sHttpConnectTimeout;
        int i2 = i > 0 ? i : 8000;
        int i3 = sHttpReadTimeout;
        int i4 = i3 > 0 ? i3 : 8000;
        Map<String, String> map = this.mMapHeadData;
        boolean equals = (map == null || map.size() <= 0) ? false : "true".equals(this.mMapHeadData.get("allowCrossProtocolRedirects"));
        if (sSkipSSLChain) {
            GSYExoHttpDataSourceFactory gSYExoHttpDataSourceFactory = new GSYExoHttpDataSourceFactory(BaseSdk.UserAgent, z ? null : new DefaultBandwidthMeter.Builder(this.mAppContext).build(), i2, i4, equals);
            Map<String, String> map2 = this.mMapHeadData;
            if (map2 != null && map2.size() > 0) {
                for (Map.Entry<String, String> entry : this.mMapHeadData.entrySet()) {
                    gSYExoHttpDataSourceFactory.getDefaultRequestProperties().set(entry.getKey(), entry.getValue());
                }
            }
            return gSYExoHttpDataSourceFactory;
        }
        DefaultHttpDataSourceFactory defaultHttpDataSourceFactory = new DefaultHttpDataSourceFactory(BaseSdk.UserAgent, z ? null : new DefaultBandwidthMeter.Builder(this.mAppContext).build(), i2, i4, equals);
        Map<String, String> map3 = this.mMapHeadData;
        if (map3 != null && map3.size() > 0) {
            for (Map.Entry<String, String> entry2 : this.mMapHeadData.entrySet()) {
                defaultHttpDataSourceFactory.getDefaultRequestProperties().set(entry2.getKey(), entry2.getValue());
            }
        }
        String valueOf = String.valueOf(System.currentTimeMillis());
        String substring = valueOf.substring(3, 8);
        defaultHttpDataSourceFactory.getDefaultRequestProperties().set("t", valueOf);
        try {
            String md5 = FileListUtils.getMD5(substring);
            HttpDataSource.RequestProperties defaultRequestProperties = defaultHttpDataSourceFactory.getDefaultRequestProperties();
            defaultRequestProperties.set("s", md5 + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        defaultHttpDataSourceFactory.getDefaultRequestProperties().set("aut", SpUtils.getInstance().getString(Key.TOKEN));
        return defaultHttpDataSourceFactory;
    }

    private static boolean resolveCacheState(Cache cache, String str) {
        if (TextUtils.isEmpty(str)) {
            return true;
        }
        String generateKey = generateKey(Uri.parse(str));
        if (!TextUtils.isEmpty(generateKey)) {
            NavigableSet<CacheSpan> cachedSpans = cache.getCachedSpans(generateKey);
            if (cachedSpans.size() != 0) {
                long j = cache.getContentMetadata(generateKey).get(ContentMetadata.KEY_CONTENT_LENGTH, -1);
                long j2 = 0;
                for (CacheSpan cacheSpan : cachedSpans) {
                    j2 += cache.getCachedLength(generateKey, cacheSpan.position, cacheSpan.length);
                }
                if (j2 >= j) {
                    return true;
                }
            }
        }
        return false;
    }

    private static String generateKey(Uri uri) {
        return uri.toString();
    }
}
